package servlet;

import entity.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/doLogin")
public class LoginServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //处理字符编码
        request.setCharacterEncoding("UTF-8");
        //获取表单的值
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String role = request.getParameter("role");//1 2 3 4
        //判断
        if("admin".equals(username)&&"123456".equals(password)){
            request.getSession().setAttribute("fullName","张三");
            request.setAttribute("userRole",role);

            List<User> users = new ArrayList<>();
            users.add(new User(1001, "小美", "xiaoMei", '女'));
            users.add(new User(1002, "小帅", "xiaoShuai", '男'));
            users.add(new User(1003, "大山", "daShan", '男'));
            users.add(new User(1004, "大壮", "daZhuang", '男'));
            request.setAttribute("users", users);

            request.getRequestDispatcher("welcome.jsp").forward(request,response);
        }else{
            request.setAttribute("errorMsg","用户名或密码错误！");
            request.getRequestDispatcher("login.jsp").forward(request,response);
        }

    }

}
